package org.pointex.common.util;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Status;

import org.pointex.util.DoPage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

/**
 * 公共的
 * @author _ervin
 *
 */
public class JsonDate {
	
	private static SerializeConfig mapping = new SerializeConfig();  
	private static String dateFormat;  
	static {
	    dateFormat = "yyyy-MM-dd HH:mm:ss";  
	    mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));  
	}
	
	/** 
     * 默认的处理时间 
     *  
     * @param jsonText 
     * @return 
     */  
    public static String toJsonString(Object obj) {  
        return JSON.toJSONString(obj,  
                SerializerFeature.WriteDateUseDateFormat);  
    }
	
	/** 
     * 默认的处理时间 
     *  
     * @param jsonText 
     * @return 
     */  
    public static JSONObject toJsonObject(Object obj) {  
        return JSONObject.parseObject(JSON.toJSONString(obj,  
                SerializerFeature.WriteDateUseDateFormat));  
    }  
  
    /** 
     * 自定义时间格式 
     *  
     * @param jsonText 
     * @return 
     */  
    public static JSONObject toJsonObject(Object jsonText,String dateFormat) {  
        mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));  
        return JSONObject.parseObject(JSON.toJSONString(jsonText, mapping));  
    }  

	
	
	
//	/**
//	 * 格式化日期
//	 * @param list
//	 * @return
//	 */
//	public static JSONObject toJsonArry(DoPage<?> list){
//		JSONObject js=new JSONObject();
//		JSONArray arr=new JSONArray();
//		List<?> lis=list.getList();
//		for (Object object : lis) {
//			arr.add(toJsonObject(object)); 
//		}
//		js.put("totalRecords",list.getTotalRecords());
//		js.put("totalPage",list.getTotalPage());
//		js.put("pageSize",list.getPageSize());
//		js.put("priverousPage",list.getPriverousPage());
//		js.put("currentPage",list.getCurrentPage());
//		js.put("nextPage",list.getNextPage());
//		js.put("TopPage",list.getTopPage());
//		js.put("BottomPage",list.getBottomPage());
//		js.put("orderColumn",list.getOrderColumn());
//		js.put("orderTurn",list.getOrderTurn());
//		js.put("map",list.getMap());
//		js.put("list",arr);
//		
//		return js;
//	}
//	
	

	
	/**
	 * 把list内部的date  转换为自定义的date格式的json
	 * @param list
	 * @return
	 */
	public static JSONArray toJsonArry(List<?> list,String format){
		JSONArray arr=new JSONArray();
		for (Object obj : list) {
			arr.add(toJsonObject(obj));
		}
		return arr;
	}
	
	public static String getRemortIP(HttpServletRequest request) { 
	    if (request.getHeader("x-forwarded-for") == null) { 
	        return request.getRemoteAddr(); 
	    } 
	    return request.getHeader("x-forwarded-for"); 
	}  
	
}